/*
 * DeleteNodeAction.java
 *
 * Created on June 9, 2007, 7:01 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.actions;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.fpdev.core.data.LegacyDB;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.BNode;
import org.fpdev.core.transit.Station;
import org.fpdev.apps.rtemaster.RouteMaster;

/**
 *
 * @author demory
 */
public class DeleteNodeAction implements ACAction {

  // info about deleted node
  private BNode node_;
  private Station station_;
  private String staNodeName_;
  
  // sub-actions for affected links
  private List<DeleteLinkAction> delLinkActions_;
  
  // update database flag
  private boolean updateDB_;

  /** Creates a new instance of DeleteNodeAction */
  public DeleteNodeAction(BNode node, boolean updateDB) { //List<CLink> delLinks) {
    node_ = node;
    updateDB_ = updateDB;
  }

  public String getName() {
    return "Delete Node";
  }

  public boolean doAction(RouteMaster av) {

    Set<BLink> linkSet = av.getEngine().getBaseNet().incidentLinks(node_);

    // check for active paths:
    for (Iterator<BLink> links = linkSet.iterator(); links.hasNext();) {
      BLink link = links.next();
      if (link.registeredPathCount() > 0) {
        av.msg("Delete failed: adjacent link belongs to active path");
        return false;
      }
    }

    delLinkActions_ = new LinkedList<DeleteLinkAction>();

    for (Iterator<BLink> links = linkSet.iterator(); links.hasNext();) {
      DeleteLinkAction dla = new DeleteLinkAction(links.next(), updateDB_);
      dla.doAction(av);
      delLinkActions_.add(dla);
    }

    av.getEngine().getBaseNet().deleteNode(node_);

    if (node_.isStation()) {
      station_ = node_.getStation();
      staNodeName_ = node_.getStaNodeName();
      node_.getStation().removeNode(node_);
    //av.getStationOps().writeStationFile();
    }
    if (updateDB_) {
      av.getEngine().getDataPackage().getSpatialDataConn().deleteNode(node_);
      //av.getEngine().getDB().deleteNode(node_);
    }
    return true;
  }

  public void updateDB(LegacyDB db) {
  /*Iterator<CLink> links = delLinks_.iterator();
  while(links.hasNext()) {
  CLink link = links.next();
  //System.out.println("delLink db "+link.getID());
  db.deleteLink(link);
  }
  db.deleteNode(node_);*/
  }

  public boolean undoAction(RouteMaster av) {
    CreateNodeAction cna = new CreateNodeAction(node_, updateDB_);
    cna.doAction(av);
    if (station_ != null) {
      station_.addNode(node_, staNodeName_);
    }

    for (Iterator<DeleteLinkAction> dlas = delLinkActions_.iterator(); dlas.hasNext();) {
      dlas.next().undoAction(av);
    }

    return true;
  }
}
